
//获取到账户名，根据账户名进行查讯信息
let name =sessionStorage.getItem("name");
if(name){
    $.ajax({
        url:`http://127.0.0.1:8088/gou?yonghu=${name}`,
        success(data){
            data.forEach(function(ietm,i){
                $(`
                    <div>
                        <ul style="background: url(${ietm.shangpinimg}) no-repeat;background-size: 194px">${ietm.id}</ul>
                        <label for="xuan${i}">
                            <input type="checkbox" id="xuan${i}"> -选则 
                        </label>
                        <li>图片ID:<span>${ietm.shangpin}</span></li>
                        <li>价格:<span class="jiage">${ietm.jiage}</span></li>
                        <li>${ietm.name}</li>
                    </div>
                `).appendTo($(".qingcenter"))
            });
            //总数
            let shu = $("#qing-con .qingcenter div").length;
            $(".qjsspan").html(shu);
            //计算那个被选上封装函数
            function han(){
                let qian = 0;
                let jiage = document.querySelectorAll("#qing-con .qingcenter .jiage")
                let input = document.querySelectorAll("#qing-con .qingcenter input")
                let zu = [];
                for(let i = 0;i < input.length;i++){
                    if(input[i].checked){
                        zu.push(input[i])
                        qian += parseFloat($(jiage[i]).html())
                    }
                }
                $(".qjsxuan").html(qian);
                $(".qjjisuan").html(zu.length);
            }

            let inp =$("#qing-con .qingcenter div input");
            inp.click(function(){
                han()
            })
            //全选与全不选
            $("#quan").click(function(){
                if(this.checked){
                    $("#qing-con .qingcenter input").prop("checked","true");
                }
                if(!this.checked){
                    $("#qing-con .qingcenter input").prop("checked","")
                };
                han();
            })
            //删除键
            $("#qing-con .shan").click(function(){
                let ul = document.querySelectorAll("#qing-con .qingcenter ul")
                let input = document.querySelectorAll("#qing-con .qingcenter input")
                let zu = [];
                for(let i = 0;i < input.length;i++){
                    if(input[i].checked){
                        zu.push(ul[i].innerHTML)
                    }
                }
                if(!zu.length){
                    alert("请选择您要删除的商品");
                    return;
                }
                if(confirm("您真的要删除吗")){
                    zu.forEach(function(ietm){
                        let http = new XMLHttpRequest();
                        http.open("get",`http://127.0.0.1:8088/goushan?id=${ietm}`);
                        http.send();
                        http.onreadystatechange = function(){
                            if(http.readyState === 4){
                                if(http.responseText === "shan"){
                                    alert("删除成功")
                                }
                                location.href = "http://127.0.0.1/zkhl/qian/gouwuche.html"
                            }
                        }
                    })

                }
            })
            //购买健
            $("#qing-con .mai").click(function(){
                $("#meng").fadeToggle();
                $("#goumai").slideToggle();
            })
            $("#goumai .mai").click(function(){
                alert("感谢购买")
                $("#meng").fadeToggle();
                $("#goumai").slideToggle();
            })
            $("#goumai .suan").click(function(){
                $("#meng").fadeToggle();
                $("#goumai").slideToggle();
            })
        },
    })
}else{
    location.href = "http://127.0.0.1/zkhl/qian/index.html"
}